package com.rbac.config;

import com.rbac.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Autowired
    @Lazy
    private PermissionInterceptor permissionInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册权限拦截器，设置较低优先级，确保在JWT认证之后执行
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/auth/login",
                        "/auth/register",
                        "/auth/logout",
                        "/auth/permissions",
                        "/auth/menus",
                        "/auth/info",
                        "/avatars/**",
                        "/error",
                        "/dashboard/**"
                )
                .order(10); // 设置较低优先级
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源映射 - 使用绝对路径确保文件能正确访问
        registry.addResourceHandler("/avatars/**")
                .addResourceLocations("file:src/main/resources/static/avatars/")
                .setCachePeriod(3600) // 设置缓存时间
                .resourceChain(true); // 启用资源链
    }
} 